import datetime

from selenium import webdriver #加载包
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions  as EC #隐式等待
from selenium.webdriver.common.keys import Keys  #换行用的

import time
class blog_system:
    def __init__(self, driver=None, driver_wait=None):
        chrome_driver = ChromeDriverManager().install()
        Options = webdriver.ChromeOptions()
        Options.page_load_strategy = "none"

        # 可以设置一下标签句柄的加载策略
        self.driver = webdriver.Chrome(service=Service(chrome_driver), options=Options)
        self.driver_wait = WebDriverWait(self.driver, 30)
        wait = self.driver.implicitly_wait(30)  # wait 7秒
        self.driver.maximize_window()

    def test_log_right(self):
        print("检测log&右上角")
        self.driver_wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                                  "body > div.header > div > div.logo > img")))

        element1 = self.driver.find_element(By.CSS_SELECTOR,
                "#header-nav > li:nth-child(1) > a")
        element2 = self.driver.find_element(By.CSS_SELECTOR,
                "#header-nav > li:nth-child(2) > a")

        if element1.get_attribute("href") != "../index.html" and element2.get_attribute("href") != "../blog_write.html":
            print("右上角地址错误")

    def test_blog_index(self):
        print("测试博客列表页")

        self.driver.get("http://139.9.104.185:7070/index.html")
        # self.test_log_right()

#        测试博客文章标题，文章简介信息
        title = self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li:nth-child(3) > div > h2 > a").text
        if title != "曹晨": print("文章一标题不正确，为:" + title)
        content = self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li:nth-child(3) > div > div").text
        if content != "曹晨喜欢玩鸣潮,也喜欢玩第五人格......": print("文章二内容不匹配：" + content)


        address = self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li:nth-child(3) > div > ul > li.btn-Msg > a").get_attribute("href")
        if address != "http://139.9.104.185:7070/cgi_sql/blog_r_sql.cgi?blogId=1013": print("阅读全文，地址不匹配：" + address)
        print("测试博客列表页完成------正常")

    def test_blog_detailed(self):
        print("测试博客详细页")

        self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li:nth-child(3) > div > ul > li.btn-Msg > a").click()
#       进入详细页
#         self.test_log_right()

        title = self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li > div").text
        content = self.driver.find_element(By.CSS_SELECTOR, "#list-left > ul > li > p:nth-child(3)").text

        if title == "曹晨": print("标题正确, 正文:" + content)

        print("测试博客详细页-------正常")

    def test_blog_del(self):
        print("开始删除文章：selenium")
        self.driver.get("http://139.9.104.185:7070/index.html")
        for i  in range(2, 8) :
            title = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(i) + ") > div > h2 > a"))
            content = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(i) + ") > div > div"))
            if title.text == "selenium" and content.text == "seleniumtest1......":
                self.driver.find_element(By.CSS_SELECTOR,
                                ("#list-left > ul > li:nth-child(" + str(i) + ") > div > ul > li:nth-child(1) > a")).click()

                print("文章删除成功")
                break
        for n  in range(2, 8) :
            title1 = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(n) + ") > div > h2 > a"))
            content1 = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(n) + ") > div > div"))
            if title1.text == "selenium" and content1.text == "seleniumtest1......":
                print("咦~还在？")
                break
        print("结束删除操作测试-------正常")
    def test_blog_write(self):
        print("测试写博客页")
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#header-nav > li:nth-child(2) > a").click()
        # self.driver.get("http://139.9.104.185:7070/blog_write.html")
        self.driver_wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,
                    "#editor > div.editormd-toolbar > div > ul > li:nth-child(1) > a > i")))

#       1.测试没有标题直接提交
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
#       会出现弹窗
        time.sleep(2)
        alert = self.driver.switch_to.alert
        alert.accept()

#       2.测试有标题
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("selenium")
        tmp = self.driver.find_element(By.CSS_SELECTOR,
        "#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre > span > span")
        print("测试:" + tmp.text)

#       3.提交
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

#       4.检查文章存在
        self.driver.find_element(By.CSS_SELECTOR, "#header-nav > li:nth-child(1) > a").click()

        # 跳转界面
        for i  in range(2, 8) :
            title = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(i) + ") > div > h2 > a"))
            content = self.driver.find_element(By.CSS_SELECTOR, ("#list-left > ul > li:nth-child(" + str(i) + ") > div > div"))
            if title.text == "selenium" and content.text == "......":
                        print("文章添加成功")
                        break

        print("写文章结束------正常")


test = blog_system()
test.test_blog_index()
test.test_blog_detailed()
test.test_blog_write()
test.test_blog_del()
test.driver.close()












